/**
 * 提交
 * */
// ===================================================================== antd
const { $fn, $Lazy } = window
const message = $Lazy.file(()=>import('@antd/message'))
// ===================================================================== 
const Index = function(option){
	const opt = {
		msg:'操作',
		...option
	}
	let { param, msg, api, onClear, onRefresh, getSubmit, refresh, onOk, onSubmit, modal, query, onComplete, okText, backRefresh, onIf } = opt
	
	if(api && !onSubmit){
		//动态获取提交参数
		if(getSubmit){
			const submit = getSubmit(param)
			this.submit = {...this.submit, ...submit}
		}
		query = typeof query === 'function' ? query(param) : query
		if(query){
			param = { ...query, ...this?.submit}
		}else{
			// 地址
			const areaData = param?.areaData ?  $fn.getAddress(param.areaData) : null
			param =  { ...this?.initSubmit, ...param, ...areaData, ...this?.submit}
		}
		
		if(onIf){
			if(!onIf(param)){
				return false
			}
		}
		//
		window.$http.submit(this, api, { param  }).then(data=>{
			// 有弹窗则关闭
			if(this?.modal && modal !== false){ this.modal.close() }
			// 提示
			message.then(f=>f.success( (okText ? okText : msg) + '成功'))
			// 回调
			$fn.delay(()=>{
				// 清除
				if(onClear){
					onClear()		
				}else{
					this?.props?.onClear?.()
				}
				//刷新
				if(onRefresh){
					onRefresh({ data, param })
				}else{
					this?.props?.onRefresh?.(data)
				}
				//
				if(refresh){
					$fn.delay(this?.fetch)
				}
				onOk?.({data, param})
				if(backRefresh){
					$fn.fresh.back.call(this)
				}
				onComplete?.({data, param})
			})
		})
	}else{
		onSubmit?.(param)
	}
}
export default Index